﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="XsdMinOccursMaxOccursComplexTypes", Namespace="http://kis.x-road.eu/", Target=typeof(XsdMinOccursMaxOccursComplexTypes))]
    public class XsdMinOccursMaxOccursComplexTypesSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected XsdMinOccursMaxOccursComplexTypes clazz;
        
        public XsdMinOccursMaxOccursComplexTypesSerializer(XsdMinOccursMaxOccursComplexTypes clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            if (((clazz.AMin1MazUnboundedNil == null) 
                        || (clazz.AMin1MazUnboundedNil.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "A_Min1_Maz_Unbounded_Nil");
            }
            for (int x = 0; ((clazz.AMin1MazUnboundedNil != null) 
                        && (x < clazz.AMin1MazUnboundedNil.Count)); x = (x + 1)) {
                Writer.WriteStartElement("A_Min1_Maz_Unbounded_Nil");
                if (clazz.AMin1MazUnboundedNil[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaAMin1MazUnboundedNil = this.GetMetaData(clazz.AMin1MazUnboundedNil[x]);
                    if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.AMin1MazUnboundedNil[x].GetType())) {
                        Writer.WriteTypeAttribute(metaAMin1MazUnboundedNil.TypeQualifiedName.Namespace, metaAMin1MazUnboundedNil.TypeQualifiedName.Name);
                    }
                    var aMin1MazUnboundedNilSerializer = metaAMin1MazUnboundedNil.NewSerializer(clazz.AMin1MazUnboundedNil[x]);
                    aMin1MazUnboundedNilSerializer.WriteXmlAttributes(Writer);
                    aMin1MazUnboundedNilSerializer.WriteXml(Writer);
                }
                else {
                    Writer.WriteAttributeString("xsi", "nil", null, "1");
                }
                Writer.WriteEndElement();
            }
            if (((clazz.AMin1MazUnboundedNonNil == null) 
                        || (clazz.AMin1MazUnboundedNonNil.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "A_Min1_Maz_Unbounded_NonNil");
            }
            for (int x = 0; ((clazz.AMin1MazUnboundedNonNil != null) 
                        && (x < clazz.AMin1MazUnboundedNonNil.Count)); x = (x + 1)) {
                Writer.WriteStartElement("A_Min1_Maz_Unbounded_NonNil");
                if (clazz.AMin1MazUnboundedNonNil[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaAMin1MazUnboundedNonNil = this.GetMetaData(clazz.AMin1MazUnboundedNonNil[x]);
                    if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.AMin1MazUnboundedNonNil[x].GetType())) {
                        Writer.WriteTypeAttribute(metaAMin1MazUnboundedNonNil.TypeQualifiedName.Namespace, metaAMin1MazUnboundedNonNil.TypeQualifiedName.Name);
                    }
                    var aMin1MazUnboundedNonNilSerializer = metaAMin1MazUnboundedNonNil.NewSerializer(clazz.AMin1MazUnboundedNonNil[x]);
                    aMin1MazUnboundedNonNilSerializer.WriteXmlAttributes(Writer);
                    aMin1MazUnboundedNonNilSerializer.WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : AMin1MazUnboundedNonNil");
                }
                Writer.WriteEndElement();
            }
            for (int x = 0; ((clazz.BMin0MazUnboundedNonNil != null) 
                        && (x < clazz.BMin0MazUnboundedNonNil.Count)); x = (x + 1)) {
                if (clazz.BMin0MazUnboundedNonNil[x]!=null) {
                    Writer.WriteStartElement("B_Min0_Maz_Unbounded_NonNil");
                    global::Xtee.Core.SerializationMetadata metaBMin0MazUnboundedNonNil = this.GetMetaData(clazz.BMin0MazUnboundedNonNil[x]);
                    if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.BMin0MazUnboundedNonNil[x].GetType())) {
                        Writer.WriteTypeAttribute(metaBMin0MazUnboundedNonNil.TypeQualifiedName.Namespace, metaBMin0MazUnboundedNonNil.TypeQualifiedName.Name);
                    }
                    var bMin0MazUnboundedNonNilSerializer = metaBMin0MazUnboundedNonNil.NewSerializer(clazz.BMin0MazUnboundedNonNil[x]);
                    bMin0MazUnboundedNonNilSerializer.WriteXmlAttributes(Writer);
                    bMin0MazUnboundedNonNilSerializer.WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            for (int x = 0; ((clazz.BMin0MazUnboundedNil != null) 
                        && (x < clazz.BMin0MazUnboundedNil.Count)); x = (x + 1)) {
                if (clazz.BMin0MazUnboundedNil[x]!=null) {
                    Writer.WriteStartElement("B_Min0_Maz_Unbounded_Nil");
                    global::Xtee.Core.SerializationMetadata metaBMin0MazUnboundedNil = this.GetMetaData(clazz.BMin0MazUnboundedNil[x]);
                    if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.BMin0MazUnboundedNil[x].GetType())) {
                        Writer.WriteTypeAttribute(metaBMin0MazUnboundedNil.TypeQualifiedName.Namespace, metaBMin0MazUnboundedNil.TypeQualifiedName.Name);
                    }
                    var bMin0MazUnboundedNilSerializer = metaBMin0MazUnboundedNil.NewSerializer(clazz.BMin0MazUnboundedNil[x]);
                    bMin0MazUnboundedNilSerializer.WriteXmlAttributes(Writer);
                    bMin0MazUnboundedNilSerializer.WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (clazz.CMin0Maz1Nil!=null) {
                Writer.WriteStartElement("C_Min0_Maz_1_Nil");
                global::Xtee.Core.SerializationMetadata metaCMin0Maz1Nil = this.GetMetaData(clazz.CMin0Maz1Nil);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.CMin0Maz1Nil.GetType())) {
                    Writer.WriteTypeAttribute(metaCMin0Maz1Nil.TypeQualifiedName.Namespace, metaCMin0Maz1Nil.TypeQualifiedName.Name);
                }
                var cMin0Maz1NilSerializer = metaCMin0Maz1Nil.NewSerializer(clazz.CMin0Maz1Nil);
                cMin0Maz1NilSerializer.WriteXmlAttributes(Writer);
                cMin0Maz1NilSerializer.WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.CMin0Maz1NonNil!=null) {
                Writer.WriteStartElement("C_Min0_Maz_1_NonNil");
                global::Xtee.Core.SerializationMetadata metaCMin0Maz1NonNil = this.GetMetaData(clazz.CMin0Maz1NonNil);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.CMin0Maz1NonNil.GetType())) {
                    Writer.WriteTypeAttribute(metaCMin0Maz1NonNil.TypeQualifiedName.Namespace, metaCMin0Maz1NonNil.TypeQualifiedName.Name);
                }
                var cMin0Maz1NonNilSerializer = metaCMin0Maz1NonNil.NewSerializer(clazz.CMin0Maz1NonNil);
                cMin0Maz1NonNilSerializer.WriteXmlAttributes(Writer);
                cMin0Maz1NonNilSerializer.WriteXml(Writer);
                Writer.WriteEndElement();
            }
            Writer.WriteStartElement("D_Min1_Maz_1_Nil");
            if (clazz.DMin1Maz1Nil!=null) {
                global::Xtee.Core.SerializationMetadata metaDMin1Maz1Nil = this.GetMetaData(clazz.DMin1Maz1Nil);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.DMin1Maz1Nil.GetType())) {
                    Writer.WriteTypeAttribute(metaDMin1Maz1Nil.TypeQualifiedName.Namespace, metaDMin1Maz1Nil.TypeQualifiedName.Name);
                }
                var dMin1Maz1NilSerializer = metaDMin1Maz1Nil.NewSerializer(clazz.DMin1Maz1Nil);
                dMin1Maz1NilSerializer.WriteXmlAttributes(Writer);
                dMin1Maz1NilSerializer.WriteXml(Writer);
            }
            else {
                Writer.WriteAttributeString("xsi", "nil", null, "1");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("D_Min1_Maz_1_NonNil");
            if (clazz.DMin1Maz1NonNil!=null) {
                global::Xtee.Core.SerializationMetadata metaDMin1Maz1NonNil = this.GetMetaData(clazz.DMin1Maz1NonNil);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Sequence), clazz.DMin1Maz1NonNil.GetType())) {
                    Writer.WriteTypeAttribute(metaDMin1Maz1NonNil.TypeQualifiedName.Namespace, metaDMin1Maz1NonNil.TypeQualifiedName.Name);
                }
                var dMin1Maz1NonNilSerializer = metaDMin1Maz1NonNil.NewSerializer(clazz.DMin1Maz1NonNil);
                dMin1Maz1NonNilSerializer.WriteXmlAttributes(Writer);
                dMin1Maz1NonNilSerializer.WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : DMin1Maz1NonNil");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            clazz.AMin1MazUnboundedNil = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Sequence>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "A_Min1_Maz_Unbounded_Nil")); ) {
                if (Reader.LocalName=="A_Min1_Maz_Unbounded_Nil") {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaAMin1MazUnboundedNil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                        global::Kis.Xroad.Eu.Sequence item = metaAMin1MazUnboundedNil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                        clazz.AMin1MazUnboundedNil.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            var aMin1MazUnboundedNilSerializer = metaAMin1MazUnboundedNil.NewSerializer(item);
                            aMin1MazUnboundedNilSerializer.ReadXmlAttributes(Reader);
                            Reader.ReadStartElement("A_Min1_Maz_Unbounded_Nil");
                            aMin1MazUnboundedNilSerializer.ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.AMin1MazUnboundedNil.Add(null);
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>XsdMinOccursMaxOccursComplexTypes>A_Min1_Maz_Unbounded_Nil", Reader.LocalName);
                }
            }
            if ((clazz.AMin1MazUnboundedNil.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "A_Min1_Maz_Unbounded_Nil");
            }
            clazz.AMin1MazUnboundedNonNil = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Sequence>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "A_Min1_Maz_Unbounded_NonNil")); ) {
                if (Reader.LocalName=="A_Min1_Maz_Unbounded_NonNil") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>XsdMinOccursMaxOccursComplexTypes>A_Min1_Maz_Unbounded_NonNil");
                    }
                    global::Xtee.Core.SerializationMetadata metaAMin1MazUnboundedNonNil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                    global::Kis.Xroad.Eu.Sequence item = metaAMin1MazUnboundedNonNil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                    clazz.AMin1MazUnboundedNonNil.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        var aMin1MazUnboundedNonNilSerializer = metaAMin1MazUnboundedNonNil.NewSerializer(item);
                        aMin1MazUnboundedNonNilSerializer.ReadXmlAttributes(Reader);
                        Reader.ReadStartElement("A_Min1_Maz_Unbounded_NonNil");
                        aMin1MazUnboundedNonNilSerializer.ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>XsdMinOccursMaxOccursComplexTypes>A_Min1_Maz_Unbounded_NonNil", Reader.LocalName);
                }
            }
            if ((clazz.AMin1MazUnboundedNonNil.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "A_Min1_Maz_Unbounded_NonNil");
            }
            if (Reader.LocalName=="B_Min0_Maz_Unbounded_NonNil") {
                clazz.BMin0MazUnboundedNonNil = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Sequence>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "B_Min0_Maz_Unbounded_NonNil")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>XsdMinOccursMaxOccursComplexTypes>B_Min0_Maz_Unbounded_NonNil");
                    }
                    global::Xtee.Core.SerializationMetadata metaBMin0MazUnboundedNonNil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                    global::Kis.Xroad.Eu.Sequence item = metaBMin0MazUnboundedNonNil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                    clazz.BMin0MazUnboundedNonNil.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        var bMin0MazUnboundedNonNilSerializer = metaBMin0MazUnboundedNonNil.NewSerializer(item);
                        bMin0MazUnboundedNonNilSerializer.ReadXmlAttributes(Reader);
                        Reader.ReadStartElement("B_Min0_Maz_Unbounded_NonNil");
                        bMin0MazUnboundedNonNilSerializer.ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="B_Min0_Maz_Unbounded_Nil") {
                clazz.BMin0MazUnboundedNil = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Sequence>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "B_Min0_Maz_Unbounded_Nil")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaBMin0MazUnboundedNil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                        global::Kis.Xroad.Eu.Sequence item = metaBMin0MazUnboundedNil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                        clazz.BMin0MazUnboundedNil.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            var bMin0MazUnboundedNilSerializer = metaBMin0MazUnboundedNil.NewSerializer(item);
                            bMin0MazUnboundedNilSerializer.ReadXmlAttributes(Reader);
                            Reader.ReadStartElement("B_Min0_Maz_Unbounded_Nil");
                            bMin0MazUnboundedNilSerializer.ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.BMin0MazUnboundedNil.Add(null);
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="C_Min0_Maz_1_Nil") {
                if ((Reader.IsElementNil() == false)) {
                    global::Xtee.Core.SerializationMetadata metaCMin0Maz1Nil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                    clazz.CMin0Maz1Nil = metaCMin0Maz1Nil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                    if ((Reader.IsEmptyElement == false)) {
                        var cMin0Maz1NilSerializer = metaCMin0Maz1Nil.NewSerializer(clazz.CMin0Maz1Nil);
                        cMin0Maz1NilSerializer.ReadXmlAttributes(Reader);
                        Reader.ReadStartElement("C_Min0_Maz_1_Nil");
                        cMin0Maz1NilSerializer.ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="C_Min0_Maz_1_NonNil") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>XsdMinOccursMaxOccursComplexTypes>C_Min0_Maz_1_NonNil");
                }
                global::Xtee.Core.SerializationMetadata metaCMin0Maz1NonNil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                clazz.CMin0Maz1NonNil = metaCMin0Maz1NonNil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                if ((Reader.IsEmptyElement == false)) {
                    var cMin0Maz1NonNilSerializer = metaCMin0Maz1NonNil.NewSerializer(clazz.CMin0Maz1NonNil);
                    cMin0Maz1NonNilSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("C_Min0_Maz_1_NonNil");
                    cMin0Maz1NonNilSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="D_Min1_Maz_1_Nil") {
                if ((Reader.IsElementNil() == false)) {
                    global::Xtee.Core.SerializationMetadata metaDMin1Maz1Nil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                    clazz.DMin1Maz1Nil = metaDMin1Maz1Nil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                    if ((Reader.IsEmptyElement == false)) {
                        var dMin1Maz1NilSerializer = metaDMin1Maz1Nil.NewSerializer(clazz.DMin1Maz1Nil);
                        dMin1Maz1NilSerializer.ReadXmlAttributes(Reader);
                        Reader.ReadStartElement("D_Min1_Maz_1_Nil");
                        dMin1Maz1NilSerializer.ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>XsdMinOccursMaxOccursComplexTypes>D_Min1_Maz_1_Nil", Reader.LocalName);
            }
            if (Reader.LocalName=="D_Min1_Maz_1_NonNil") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>XsdMinOccursMaxOccursComplexTypes>D_Min1_Maz_1_NonNil");
                }
                global::Xtee.Core.SerializationMetadata metaDMin1Maz1NonNil = this.GetMetaData<global::Kis.Xroad.Eu.Sequence>(Reader, "Sequence", "http://kis.x-road.eu/");
                clazz.DMin1Maz1NonNil = metaDMin1Maz1NonNil.NewEntity<global::Kis.Xroad.Eu.Sequence>();
                if ((Reader.IsEmptyElement == false)) {
                    var dMin1Maz1NonNilSerializer = metaDMin1Maz1NonNil.NewSerializer(clazz.DMin1Maz1NonNil);
                    dMin1Maz1NonNilSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("D_Min1_Maz_1_NonNil");
                    dMin1Maz1NonNilSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>XsdMinOccursMaxOccursComplexTypes>D_Min1_Maz_1_NonNil", Reader.LocalName);
            }
        }
    }
}
